from by.vaukalak.core.net.commands.Command import Command;

class CommandSerializer:

    def __init__(self):
        pass;

    def serialize(self, command):
        result = "";
        result += command.name + "?";
        result += self._serializeArray(command.params);
        return result;

    def deserialize(self, string):
        arr = string.split("?");
        name = arr[0];
        return Command(name, self._processArray(arr[1]));

    #serialize utils

    def _serializeArray(self, arr):
        i = 0;
        result = "[";
        while(i<len(arr)):
            if(isinstance(arr[i], (list, tuple))):
                result += self._serializeArray(arr[i]);
            elif (isinstance(arr[i], (dict))):
                result += self._serializeHash(arr[i]);
            else:
                result += self._serializeString(arr[i]);
            if(i < len(arr) - 1):
                 result += ",";
            i += 1;
        return result + "]";

    def _serializeHash(self, obj):
        result = "{";
        for key in obj:
            result = result + key + ":" + obj[key] + ",";
        result = result[0:len(result) - 1];
        return result + "}";

    def _serializeString(self, string):
        string = str(string);
        return string;

    #deserialize utils

    def _processArray(self, string):
        result = list();
        if(string[0] == "["):
            i = self._findCloseBracketsIndex(string);
            string = string[1:i];
        processComplete = False;
        while(processComplete == False and len(string) > 0):
            if(string[0] == "["):
                result.append(self._processArray(string));
                i = self._findCloseBracketsIndex(string);
                if(i>=len(string)):
                    processComplete = True;
                else:
                    string = string[i + 2:len(string)];
            elif(string[0] == "{"):
                result.append(self._processHash(string));
                i = self._findCloseFigureIndex(string);
                if(i>=len(string)):
                    processComplete = True;
                else:
                    string = string[i + 2:len(string)];
            else:
                args = string.split(",", 1);
                result.append(args[0]);
                if(len(args) == 1):
                    processComplete = True;
                else:
                    string = args[1];
        return result;

    def _processHash(self, string):
        result = {};
        if(string[0] == "{"):
            i = self._findCloseFigureIndex(string);
            string = string[1:i];
        while(string != None):
            args = string.split(",", 1);
            pair = args[0].split(":")
            result[pair[0]] = pair[1];
            if(len(args)==1):
                string = None;
            else:
                string = args[1];
        return result;


    def _findCloseFigureIndex(self, string):
        i = 1;
        nBrackets = 1;
        while(nBrackets != 0 and i < len(string)):
            if(string[i] == "{"):
                nBrackets+=1;
            else:
                if(string[i] == "}"):
                    nBrackets-=1;
            i += 1;
        return i - 1;

    def _findCloseBracketsIndex(self, string):
        i = 1;
        nBrackets = 1;
        while(nBrackets != 0 and i < len(string)):
            if(string[i] == "["):
                nBrackets+=1;
            else:
                if(string[i] == "]"):
                    nBrackets-=1;
            i += 1;
        return i - 1;